package org.wlgzs.shop.controller.page;

import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.wlgzs.shop.bean.Msg;
import org.wlgzs.shop.bean.OrderTransportData;
import org.wlgzs.shop.bean.ShowDate;
import org.wlgzs.shop.bean.UserDate;
import org.wlgzs.shop.config.AlipayConfig;
import org.wlgzs.shop.dao.GoodsDataMapper;
import org.wlgzs.shop.service.IGoodsDateService;
import org.wlgzs.shop.service.IManagerKind;
import org.wlgzs.shop.service.IOrderTransportDateService;
import org.wlgzs.shop.service.IStoreService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
public class twoController {

    @Autowired
    IGoodsDateService goodsDateService;

    @Autowired
    IStoreService storeService;

    @Autowired
    GoodsDataMapper goodsDataMapper;

    @Autowired
    IManagerKind managerKind;

    @Autowired
    IOrderTransportDateService iOrderTransportDateService;

    @RequestMapping("/common")
    public String jumpCommon(Model model, @Param("oneKindId") int oneKindId, @Param("kindStoreId") String kindStoreId, @Param("kindTwoId") String kindTwoId, @RequestParam(value = "page", defaultValue = "1")Integer page){
        PageHelper.startPage(page,15);
        List<ShowDate> list=goodsDateService.selectByStoreTwoKind(oneKindId,kindStoreId,kindTwoId);
        PageInfo pageInfo=new PageInfo(list,15);
        model.addAttribute("goodsList",pageInfo);
        model.addAttribute("storeDate",storeService.selectStore(oneKindId));
        model.addAttribute("kindTwo",managerKind.oneContainsTwo(oneKindId+""));
        model.addAttribute("kindOne",managerKind.oneKind());
        return "special/common";
    }


    @RequestMapping("/buyGoods")
    public String buyGoods( HttpSession session, HttpServletRequest request) throws UnsupportedEncodingException, AlipayApiException {
        Map<String, String> params = new HashMap<String, String>();
        Map<String, String[]> requestParams = request.getParameterMap();
        for (Iterator<String> iter = requestParams.keySet().iterator(); iter.hasNext(); ) {
            String name = (String) iter.next();
            String[] values = (String[]) requestParams.get(name);
            String valueStr = "";
            for (int i = 0; i < values.length; i++) {
                valueStr = (i == values.length - 1) ? valueStr + values[i]
                        : valueStr + values[i] + ",";
            }
            //乱码解决，这段代码在出现乱码时使用
            valueStr = new String(valueStr.getBytes("ISO-8859-1"), "utf-8");
            params.put(name, valueStr);
        }

        boolean signVerified = AlipaySignature.rsaCheckV1(params, AlipayConfig.alipay_public_key, AlipayConfig.charset, AlipayConfig.sign_type); //调用SDK验证签名

        //——请在这里编写您的程序（以下代码仅作参考）——
            //商户订单号
            String out_trade_no = new String(request.getParameter("out_trade_no").getBytes("ISO-8859-1"), "UTF-8");

            //支付宝交易号
            String trade_no = new String(request.getParameter("trade_no").getBytes("ISO-8859-1"), "UTF-8");

            //付款金额
            String total_amount = new String(request.getParameter("total_amount").getBytes("ISO-8859-1"), "UTF-8");

        System.out.println(out_trade_no);
        System.out.println(total_amount);
        UserDate userDate = (UserDate) session.getAttribute("user");
        System.out.println(userDate.getUserId());
         OrderTransportData orderTransportData=new OrderTransportData();
         orderTransportData.setOrderId(Integer.parseInt(out_trade_no ));
         orderTransportData.setUserId(userDate.getUserId());;
         Calendar calendar=Calendar.getInstance();
         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
         orderTransportData.setConsignTime(format.format(calendar.getTime()));
         orderTransportData.setPrice(Float.parseFloat(total_amount));
         String goodsId=out_trade_no.substring(6,out_trade_no.length());
         String photo=goodsDateService.selectGoods(Integer.parseInt(goodsId)).getPhotoUrl();
         orderTransportData.setEndTime(goodsId);
         orderTransportData.setPicPath(photo);
         iOrderTransportDateService.insertOrderTransportDataService(orderTransportData);
         return "redirect:/myOrder";
    }
    @RequestMapping("/watch")
    public String jumpWatch(){
        return "special/watch";
    }
}
